package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.WithdrawDepositConverter;
import com.dycx.user.dto.WalletLogDTO;
import com.dycx.user.dto.WithdrawDepositDTO;
import com.dycx.user.entity.UserInfoDO;
import com.dycx.user.entity.WalletLogDO;
import com.dycx.user.entity.WithdrawDepositDO;
import com.dycx.user.enums.TransactionTypeEnum;
import com.dycx.user.enums.WithdrawDepositStatusEnum;
import com.dycx.user.mapper.WithdrawDepositMapper;
import com.dycx.user.service.UserInfoService;
import com.dycx.user.service.WalletLogService;
import com.dycx.user.service.WithdrawDepositService;
import com.dycx.user.vo.UserInfoVO;
import com.dycx.user.vo.UserVO;
import com.dycx.user.vo.WithdrawDepositVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资金明细(WithdrawDepositApply)表服务实现类
 *
 * @author wuzongbing
 * @since 2024-08-22 01:03:00
 */

@Service
@AllArgsConstructor
public class WithdrawDepositImpl extends ServiceImpl<WithdrawDepositMapper, WithdrawDepositDO> implements WithdrawDepositService {

    private WithdrawDepositMapper withdrawDepositMapper;
    private UserInfoService userInfoService;
    private WalletLogService walletLogService;


    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<WithdrawDepositVO> getPage(WithdrawDepositDTO dto) {
        LambdaQueryWrapper<WithdrawDepositDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), WithdrawDepositDO::getId, dto.getId());
        wrapper.orderByDesc(WithdrawDepositDO::getId);

        Page<WithdrawDepositDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<WithdrawDepositVO> voPage = WithdrawDepositConverter.entityToVoPage(entityPage);
        voPage.setRecords(setListData(voPage.getRecords(), dto));

        return voPage;
    }


    /**
     * 设置数据
     *
     * @param voList 实体
     * @return 查询结果
     */
    public List<WithdrawDepositVO> setListData(List<WithdrawDepositVO> voList, WithdrawDepositDTO dto) {
        return voList;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public WithdrawDepositVO load(Integer id) {
        WithdrawDepositDO entity = getById(id);

        return WithdrawDepositConverter.entityToVo(entity);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(WithdrawDepositDTO dto) {
        if (dto.getAmount() < 50) {
            throw new RuntimeException("可提现余额不足，最低50元可提现");
        }

        UserInfoVO userInfoVO = userInfoService.load(dto.getUid());
        if (dto.getAmount() > userInfoVO.getAccountBalance()) {
            throw new RuntimeException("取现金额不能大于账户余额");
        }

        LambdaQueryWrapper<WithdrawDepositDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WithdrawDepositDO::getUid, dto.getUid());
        wrapper.eq(WithdrawDepositDO::getResult, WithdrawDepositStatusEnum.APPLY.getCode());
        if (count(wrapper) > 0) throw new RuntimeException("已有提现申请，请勿重复提交");

        dto.setResult(WithdrawDepositStatusEnum.APPLY.getCode());
        WithdrawDepositDO entity = BeanUtil.toBean(dto, WithdrawDepositDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Boolean edit(WithdrawDepositDTO dto) {
        WithdrawDepositVO vo = load(dto.getId());

        WithdrawDepositDO entity = new WithdrawDepositDO();
        entity.setId(dto.getId());
        entity.setResult(dto.getResult());
        entity.setRemark(dto.getRemark());

        if (!updateById(entity)) throw new RuntimeException("修改申请失败");

        if (dto.getResult().equals(WithdrawDepositStatusEnum.SUCCESS.getCode())) {
            UserInfoVO userInfoVO = userInfoService.load(dto.getUid());

            UserInfoDO userInfoDO = new UserInfoDO();
            userInfoDO.setUid(dto.getUid());
            userInfoDO.setAccountBalance(userInfoVO.getAccountBalance() - vo.getAmount());
            userInfoDO.setWithdrawalAmount(userInfoVO.getWithdrawalAmount() + vo.getAmount());
            if (!userInfoService.edit(userInfoDO)) throw new RuntimeException("修改金额失败");

            WalletLogDO walletLogDO = new WalletLogDO();
            walletLogDO.setUid(vo.getUid());
            walletLogDO.setTransactionType(TransactionTypeEnum.WITHDRAWAL.getCode());
            walletLogDO.setTitle("余额提现");
            walletLogDO.setAmount(-vo.getAmount());
            walletLogDO.setAccountBalance(userInfoVO.getAccountBalance() - vo.getAmount());
            walletLogDO.setOutTradeNo(dto.getOutTradeNo());

            if (!walletLogService.save(walletLogDO)) throw new RuntimeException("添加日志失败");
        }

        return true;
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return withdrawDepositMapper.getMaxSort();
    }
}

